package com.tueye.framework.core.template.impl;

import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.tueye.framework.core.template.ITemplate;
import com.tueye.framework.core.template.support.FreeMarkerConfigurationFactory;
import com.tueye.framework.core.template.support.FreeMarkerTemplateUtils;

import freemarker.template.TemplateException;

/**
 * 类说明：模板组件实现类
 * 
 * @author 韩晓伟
 * 
 */
@Component("iTemplate")
public class TemplateImpl implements ITemplate {

	private final Log logger = LogFactory.getLog(getClass());
	private FreeMarkerConfigurationFactory templateConfigurationFactory;

	@Override
	public String processTemplateIntoString(String name, Object dataModel) {
		try {
			return FreeMarkerTemplateUtils.processTemplateIntoString(
					templateConfigurationFactory.getTemplate(name), dataModel);
		} catch (IOException e) {
			logger.error("读取模板资源文件出错", e);
		} catch (TemplateException e) {
			logger.error("处理模板文件时出错", e);
		}
		return null;
	}

	@Autowired
	public void setTemplateConfigurationFactory(
			@Qualifier("frameworkTemplateConfigurationFactory")
			FreeMarkerConfigurationFactory templateConfigurationFactory) {
		this.templateConfigurationFactory = templateConfigurationFactory;
	}
}
